package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.Supplier;
import com.gxa.zhonglong.service.SupplierService;
import com.gxa.zhonglong.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author NITRO
 */
@Api
@CrossOrigin
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService=null;

    @ApiOperation(value = "查询供应商")
    @PostMapping("/getSupplierList")
            @ApiImplicitParams({
                    @ApiImplicitParam(name = "name",value = "供应商名称",required =false,dataType = "String",paramType = "query"),
                    @ApiImplicitParam(name = "page",value = "当前页码",required =true,dataType = "Integer",paramType = "query"),
                    @ApiImplicitParam(name = "size",value = "每页条数",required =true,dataType = "Integer",paramType = "query")
            })
    public Map getSupplierList(@RequestParam("page") Integer page,
                       @RequestParam("size")Integer size,
                       @RequestParam("name")String name ){
        List<Supplier> select=supplierService.select(page,size,name);
        List<Supplier> suppliers = supplierService.selectAll();
        int count = suppliers.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.put("count",count);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("addSupplier")
    public Map addSupplier( Supplier supplier) throws BusinessException {
        JsonResult jsonResult=new JsonResult();

        supplierService.add(supplier);
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "删除供应商")
    @PostMapping("deleteSupplier")
    public Map<String,Object> deleteSupplier(@RequestBody Map<String,List<Long>> ids) throws BusinessException {
        supplierService.delete(ids.get("ids"));
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "修改供应商")
    @PostMapping("updateSupplier")
    public Map updateSupplier( Supplier supplier) throws BusinessException {
        JsonResult jsonResult=new JsonResult();

        supplierService.update(supplier);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @ApiOperation(value = "查询供应商")
    @PostMapping("/getSupplierAllList")
    public Map getSupplierListAll( ){
        List<Supplier> select=supplierService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(select);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
}
